import FormContext from './context';
import { Component } from 'react';
import React from 'react';
export default class Field extends Component {
  static contextType = FormContext;
  componentWillMount() {
    const { registerEneity } = this.context;
    this.cancelRegister = registerEneity(this);
  }

  componentWillUnmount() {
    this.cancelRegister();
  }

  filedFourceUpdate() {
    this.forceUpdate();
  }

  getControled = () => {
    const { name } = this.props;
    const { getFieldValue, setFieldsValue } = this.context;
    return {
      value: getFieldValue(name) || '',
      onChange: (e) => {
        setFieldsValue({ [name]: e.target.value });
      },
    };
  };
  render() {
    const { children } = this.props;
    const { label } = this.props;
    const newChildren = React.cloneElement(
      children,

      {
        ...this.getControled(),
      },
    );
    return (
      <>
        {label}
        {newChildren}
      </>
    );
  }
}
